const express = require('express');
const proxy = require("http-proxy-middleware");
const app = express();
const cors = require('cors')

app.all('*', function (req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
    res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');


    if (req.method == 'OPTIONS') {
        res.send(200);
        /让options请求快速返回/
    } else {
        next();
    }
});

app.use(cors({
    origin: ['http://localhost:8090'],
    methods: ['GET', 'POST'],
    // allowedHeaders: ['Conten-Type']
}))

app.use('/mini/**',
    proxy({
        // 代理目标地址
        // target: "https://api.erongbank.com",
        target: "http://dev.erongbank.com",
        changeOrigin: true,
        secure: false,
        //   pathRewrite: {
        //       // 地址重写
        //       "/mini": "/"
        //   }
    }));

app.use('/api/**',
    proxy({
        // 代理目标地址
        // target: "https://api.erongbank.com",
        target: 'http://dev.erongbank.com',
        changeOrigin: true,
        secure: false,
        //   pathRewrite: {
        //       // 地址重写
        //       "/mini": "/"
        //   }
    }));

app.use('/getSignPackage',
    proxy({
        // 代理目标地址
        // target: "http://localhost:8091",
        target: "http://wechat-api-test.erongbank.com",
        changeOrigin: true,
        secure: false,
        //   pathRewrite: {
        //       // 地址重写
        //       "/mini": "/"
        //   }
    }));

// app.use("/",
//     proxy({
//         // 这里是vue/react启动后需要访问网页的地址
//         // **修改了这里
//         target: "http://localhost:10087",
//         changeOrigin: true,
//     }))


app.get("/test", (req, res) => {
    res.send({
        data: "我来组成测试数据！"
    });
})

app.listen(8090, () => {
    console.log("项目启动与：http://localhost:8090");
})